package db_main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func GetLotteryById(id int) (*models_main.Lottery, error) {
	fName := "GetLotteryById"
	o := &models_main.Lottery{}
	err := mysql.GetRTable(&models_main.Lottery{}).First(&o, id).Error
	if errors.Is(err, gorm.ErrRecordNotFound) {
		err = nil
	}
	if err != nil {
		err = fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		return nil, err
	}
	return o, nil
}

func GetEffectiveLotteryList() ([]models_main.Lottery, error) {
	fName := "GetEffectiveLotteryList"
	var o []models_main.Lottery
	err := mysql.GetRTable(&models_main.Lottery{}).
		Where("act_st_time <= now() and act_ed_time >= now()").
		Find(&o).Error
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	return o, nil
}
